package com.wisnews.util;

import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;


/**
 * <p>
 *  日期处理类
 * </p>
 *
 * @author yuyd
 * @since 2020-12-15
 */
public final class DateUtil {

	private DateUtil() {
	}

	/** 时间格式(yyyy-MM-dd) */
	public final static String DATE_YYYYMMDD = "yyyyMMdd";
	/** 时间格式(yyyy-MM-dd) */
	public final static String DATE_PATTERN = "yyyy-MM-dd";
	/** 时间格式(yyyy-MM-dd HH:mm:ss) */
	public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
	/**
	 * @Fields CS_TIME_PATTERN : 时间格式(dd HH:mm)
	 */
	public final static String CS_TIME_PATTERN = "dd HH:mm";
	/**
	 * @Fields XLS_DATE_PATTERN : 时间格式(yyyy/MM/dd)
	 */
	public final static String XLS_DATE_PATTERN = "yyyy/MM/dd";

	public static String format(Date date) {
		return format(date, DATE_PATTERN);
	}

	public static String format(Date date, String pattern) {
		if (date != null) {
			return new SimpleDateFormat(pattern).format(date);
		}
		return null;
	}

	public static Date parse(String date, String parseTo) {
		if (StringUtils.isBlank(parseTo)) {
			parseTo = DATE_TIME_PATTERN;
		}
		try {
			return new SimpleDateFormat(parseTo).parse(date);
		} catch (ParseException e) {
			return new Date();
		}
	}

	public static String thisWeekBegin(String pattern) {
		return weekDay(null, pattern, Calendar.MONDAY);
	}

	public static String weekDay(Date date, String pattern, int dayOfWeek) {
		Calendar cal = Calendar.getInstance();
		if (!Objects.isNull(date)) {
			cal.setTime(date);
		}
		cal.setFirstDayOfWeek(dayOfWeek);
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
		if (dayWeek == 1) {
			dayWeek = 8;
		}
		cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);
		if (StringUtils.isBlank(pattern)) {
			pattern = DATE_YYYYMMDD;
		}
		return new SimpleDateFormat(pattern).format(cal.getTime());
	}

	public static Date monthFirstDateTime(Date date, int offset) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(null == date ? new Date() : date);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + offset);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	public static Date monthEndDateTime(Date date, int offset) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(null == date ? new Date() : date);
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + offset);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	public static Date offestDays(Date date, int offest) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH, offest);
		return cal.getTime();
	}

	/**
	 * 获取当前时间
	 * 
	 * @return
	 */
	public static Timestamp getCurrentTimeStamp() {
		return Timestamp.valueOf(new SimpleDateFormat(DATE_TIME_PATTERN).format(new Date()));
	}

	/**
	 * 获取当前时间
	 * 
	 * @return
	 */
	public static LocalDateTime getLocalDateTime() {
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_TIME_PATTERN);
		String nowTime = new SimpleDateFormat(DATE_TIME_PATTERN).format(new Date());
		LocalDateTime dateTime = LocalDateTime.parse(nowTime, formatter);
		return dateTime;
	}


	/**
	 * 获取当前时间 datatime
	 *
	 * @return
	 */
	public static String getCurrentDateTime() {
		return new SimpleDateFormat(DATE_TIME_PATTERN).format(new Date());
	}

}
